# -*- coding: iso-8859-15 -*-

#    SuperClipboard is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.

import WindowSettings
import WindowAbout
import WindowStaticCopy
import gtk

class Gui:
    def __init__(self,copier):
        self.copier = copier
        self.build_trayicon()
        self.windows=[]

#TrayIcon      
    def build_trayicon(self):
        self.trayicon = gtk.StatusIcon()
        self.trayicon.set_from_file("./img/ico.png")
        self.trayicon.set_visible(True)
        self.trayicon.connect("button-press-event",self.trayicon_activate_callback)

    def get_menu_position(self,menu):
        rect = self.trayicon.get_geometry()[1]
        x = rect.x
        y = rect.y + self.trayicon.get_size()
        return x,y,False
    
    def build_menu(self):
        menu = gtk.Menu()
        for element in self.copier.get_element():
            str_element = str(element)[0:30]
            item_element = gtk.RadioMenuItem(None,str_element)
            
            if self.copier.clipboard.wait_for_text() == element:
                item_element.activate()
            
            item_element.connect("button-press-event",self.static_menu_press_callback,element)
            menu.append(item_element)
            item_element.set_tooltip_text(element)
            
        menu.append(gtk.SeparatorMenuItem())    
        if len(self.copier.config.staticcopy) > 0:    
            submenu_staticcopy = gtk.Menu()
            for element in self.copier.config.staticcopy:
                str_element = str(element)[0:30]
                item_element = gtk.RadioMenuItem(None,str_element)
                
                if self.copier.clipboard.wait_for_text() == element:
                    item_element.activate()
                
                item_element.connect("button-press-event",self.static_menu_press_callback,element)
                submenu_staticcopy.append(item_element)
                item_element.set_tooltip_text(element)
                
            menu_staticcopy = gtk.MenuItem('Static')
            menu_staticcopy.set_submenu(submenu_staticcopy)
            menu.append(menu_staticcopy)
            menu.append(gtk.SeparatorMenuItem())
            
        for client in self.copier.runningclient:
            submenu_cli = gtk.Menu()
            for element in client.listelement:
                str_element = str(element)[0:30]
                item_element = gtk.RadioMenuItem(None,str_element)
                
                if self.copier.clipboard.wait_for_text() == element:
                    item_element.activate()
                
                item_element.connect("button-press-event",self.static_menu_press_callback,element)
                submenu_cli.append(item_element)
                item_element.set_tooltip_text(element)
                
            menu_cli = gtk.MenuItem(client.cliconfig['name'])
            menu_cli.set_submenu(submenu_cli)
            menu.append(menu_cli)
            menu.append(gtk.SeparatorMenuItem())
        
        item_quit = gtk.MenuItem("Settings")
        item_quit.connect("activate",self.settingswindow_callback)
        menu.append(item_quit) 
        
        item_quit = gtk.MenuItem("About")
        item_quit.connect("activate",self.about_callback)
        menu.append(item_quit)  
        
        item_quit = gtk.MenuItem("Quit")
        item_quit.connect("activate",self.copier.exit)
        menu.append(item_quit)  
        
        menu.show_all()
        self.trayicon.traymenu = menu
#Fonction trayicon
    def check_opened_window(self,object):
        for window in self.windows:
            if isinstance(window,object):
                width, height = window.get_size()
                window.move(gtk.gdk.screen_width() - width, gtk.gdk.screen_height() - height)
                window.set_keep_above(True)
                window.set_keep_above(False)
                return True
        return False
    
    def open_menu(self,button,time):
        self.build_menu()
        if self.copier.config.iswindows():
            self.trayicon.traymenu.popup(None,None,self.get_menu_position,button,time)
        else:
            self.trayicon.traymenu.popup(None,None,self.get_menu_position,button,time)
            
    def open_static_menu(self):
        if not self.copier.gui.check_opened_window(WindowStaticCopy.WindowStaticCopy):
            WindowStaticCopy.WindowStaticCopy(self.copier)
            
#Fonction extern
    def error(self, info_error):
#        error = gtk.MessageDialog(None,0, gtk.MESSAGE_ERROR,gtk.BUTTONS_CLOSE, info_error)
#        error.run()
#        error.destroy()
        print "Error: "+info_error
  
            
#CallBack
    def static_menu_press_callback(self,widget,event,element):
        self.copier.set_element_to_clipboard(None,element)

    def trayicon_activate_callback(self,widget=None,event=None,element=None):
        if self.copier.config.get('InverseClick') == False:
            if event.button == 1:
                self.open_static_menu()
            elif event.button ==3:
                self.open_menu(event.button,event.time)
        else:
            if event.button == 3:
                self.open_static_menu()
            elif event.button ==1:
                self.open_menu(event.button,event.time)
        
    def settingswindow_callback(self,widget=None):
        if not self.copier.gui.check_opened_window(WindowSettings.WindowSettings):
            WindowSettings.WindowSettings(self.copier)
        
    def about_callback(self,widget=None):
        if not self.check_opened_window(WindowAbout.WindowAbout):
            WindowAbout.WindowAbout(self.copier)